package com.jcfk.eam.controller.mt;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.domain.dto.base.EamSopItemDTO;
import com.jcfk.eam.domain.dto.base.EamSopStandardDTO;
import com.jcfk.eam.domain.dto.mt.EamMtCalibrateDetailDTO;
import com.jcfk.eam.domain.dto.mt.EamMtCalibrateOrderDTO;
import com.jcfk.eam.domain.query.base.EamSopItemQuery;
import com.jcfk.eam.domain.query.base.EamSopStandardQuery;
import com.jcfk.eam.domain.query.mt.EamMtCalibrateOrderQuery;
import com.jcfk.eam.service.base.EamSopItemService;
import com.jcfk.eam.service.base.EamSopStandardService;
import com.jcfk.eam.service.mt.EamMtCalibrateOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 量检具标定工单
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-09-11
 */
@Api(tags = {"量检具标定工单管理"})
@RestController
@RequestMapping("eammtcalibrateorder")
public class EamMtCalibrateOrderController {
    @Autowired
    private EamMtCalibrateOrderService eamMtCalibrateOrderService;
    @Autowired
    private EamSopStandardService eamSopStandardService;
    @Autowired
    private EamSopItemService eamSopItemService;

    /**
     * 分页列表
     */
    @ApiOperation("分页查询量检具标定工单")
    @PostMapping("/queryByPage")
    public RetPage<EamMtCalibrateOrderDTO> queryByPage(@RequestBody ReqPageParam<EamMtCalibrateOrderQuery> requestParam) {
        DataPage<EamMtCalibrateOrderDTO> page = new DataPage<EamMtCalibrateOrderDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamMtCalibrateOrderDTO.class);
        page = eamMtCalibrateOrderService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取量检具标定工单列表", notes = "获取量检具标定工单列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamMtCalibrateDetailDTO>> getDetails(@RequestBody EamMtCalibrateOrderQuery query) {

        List<EamMtCalibrateDetailDTO> list = eamMtCalibrateOrderService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取量检具标定工单列表", notes = "获取量检具标定工单列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamMtCalibrateOrderDTO>> getList(@RequestBody EamMtCalibrateOrderQuery query) {

        List<EamMtCalibrateOrderDTO> list = eamMtCalibrateOrderService.getList(query);

        return RetResult.ok(list);
    }

    /**
     *  量检具标定工单
     * 根据id 查询详情
     */
    @ApiOperation("查询量检具标定工单")
    @GetMapping("/get/{id}")
    public RetResult<EamMtCalibrateOrderDTO> get(@PathVariable("id") String id) {
        EamMtCalibrateOrderDTO item = eamMtCalibrateOrderService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理量检具标定工单
     */
    @ApiOperation("保存量检具标定工单")
    @PostMapping("/save")
    @RequestLogHeader(modular = "量检具标定工单管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamMtCalibrateOrderDTO eamMtCalibrateOrder) {
        // 数据校验
        RetResult ret = eamMtCalibrateOrderService.isValid(eamMtCalibrateOrder);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamMtCalibrateOrder.getId())){
            // 新增
                eamMtCalibrateOrderService.insert(eamMtCalibrateOrder);
        } else{
            // 更新
                eamMtCalibrateOrderService.update(eamMtCalibrateOrder);
        }
        return RetResult.ok();

    }


    /**
     *  批量删除量检具标定工单
     */
    @ApiOperation("批量删除量检具标定工单")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "量检具标定工单管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamMtCalibrateOrderDTO eamMtCalibrateOrder) {
        return RetResult.ok(eamMtCalibrateOrderService.removeBigBatch(eamMtCalibrateOrder.getIds()));
    }

    /**
     *  删除量检具标定工单
     */
    @ApiOperation("删除量检具标定工单")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "量检具标定工单管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamMtCalibrateOrderService.deleteitem(id);
        return RetResult.ok();
    }

    /**
     * 获取标定标准列表
     * @param eamSopStandardQuery
     */
    @ApiOperation("获取标定标准列表")
    @PostMapping("getStandardList")
    public RetResult<?> getStandardList(@RequestBody EamSopStandardQuery eamSopStandardQuery){
        eamSopStandardQuery.setAssetKind(2);
        eamSopStandardQuery.setTaskKind(4);
        List<EamSopStandardDTO> list=eamSopStandardService.getStandardList(eamSopStandardQuery.getAssetKind(),eamSopStandardQuery.getTaskKind(),eamSopStandardQuery.getEquipTypeId());
        return RetResult.ok(list);
    }

    /**
     * 获取标定项目列表
     * @param standardId
     */
    @ApiOperation("获取标定项目列表")
    @GetMapping("getStandardItemList/{standardId}")
    public RetResult<?> getStandardItemList(@PathVariable("standardId") String standardId){
        EamSopItemQuery query=new EamSopItemQuery();
        query.setStandardId(standardId);
        List<EamSopItemDTO> list=eamSopItemService.getList(query);
        return RetResult.ok(list);
    }
}
